package com.school.edupoint.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.school.edupoint.model.Activity;
import com.school.edupoint.model.Gift;
import com.school.edupoint.model.StudentActivityHistory;
import com.school.edupoint.response.Result;
import com.school.edupoint.service.ActivityService;
import com.school.edupoint.service.GiftService;
import com.school.edupoint.service.StudentActivityHistoryService;
import com.school.edupoint.service.StudentService;
import com.school.edupoint.vo.HomeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class HomeController {

    @Autowired
    private StudentActivityHistoryService studentActivityHistoryService;
    @Autowired
    private StudentService studentService;
    @Autowired
    private ActivityService activityService;
    @Autowired
    private GiftService giftService;

    /**
     * 首页头部
     *
     * @return 总报名人数、总签到人数、总签到率、积分总量
     * @throws Exception
     */
    @PostMapping("/home/head")
    public Result<HomeVO> head() throws Exception {
        HomeVO homeVO = studentActivityHistoryService.selectInfo();
        // 积分总量
        Integer totalPoints = studentActivityHistoryService.selectTotalPoints();
        homeVO.setTotalPoints(totalPoints);

        System.out.println("home中总签到人数返回数据为" + homeVO.getTotalCheckIn());
        System.out.println("home中签到率（浮点数）返回数据为" + homeVO.getCheckInRate());
        System.out.println("home中总报名人数返回数据为" + homeVO.getTotalRecords());
        System.out.println("home中签到率（格式化百分比字符串）返回数据为" + homeVO.getCheckInRateStr());
        System.out.println("home中积分总量返回数据为" + homeVO.getTotalPoints());

        return Result.success(homeVO);
    }

    /**
     * 首页接口身体
     * @return
     * @throws Exception
     */
    @PostMapping("/home/body")
    public Result<HomeVO> body() throws Exception {
        HomeVO homeVO = new HomeVO();
        // 总用户数量
        homeVO.setStudentCount(studentService.selectAll());
        // 近24小时新增的用户数量
        homeVO.setNewUsersWithin24h(studentService.selectNewUsersWithIn24h());
        // 活动历史表总行数
        homeVO.setHistoryActivityCount(activityService.selectAll());
        // 历史活动中状态为2的总条数
        homeVO.setStatusTwoCount(studentActivityHistoryService.selectStatusTwoCount());
        // 历史活动中状态为2的积分总和
        homeVO.setStatusTwoPointCount(studentActivityHistoryService.selectStatusTwoPointCount());

        // 调用新增的查询方法获取参加人数最多的活动和兑换人数最多的礼物
        homeVO.setTopActivities(studentActivityHistoryService.selectTopActivities(5));

        homeVO.setTopGifts(giftService.selectTopGifts(5));

        // 打印日志
        System.out.println("总用户数量: " + homeVO.getStudentCount());
        System.out.println("近24小时新增用户: " + homeVO.getNewUsersWithin24h());
        System.out.println("活动历史总行数: " + homeVO.getHistoryActivityCount());
        System.out.println("状态为2的活动总数: " + homeVO.getStatusTwoCount());
        System.out.println("状态为2的活动积分总和: " + homeVO.getStatusTwoPointCount());
        System.out.println("参加人数最多的活动: " + homeVO.getTopActivities());
        System.out.println("兑换人数最多的礼物: " + homeVO.getTopGifts());

        return Result.success(homeVO);
    }
    /**
     * 最受欢迎的活动
     *
     * @return
     * @throws Exception
     */
    @PostMapping("/home/PA")
    public Result<List<Activity>> PA() throws Exception {
        return Result.success(studentActivityHistoryService.PA());
    }

    /**
     * 最受欢迎的礼物
     *
     */
    @PostMapping("/home/PGift")
    public Result<List<Gift>> PGift() throws Exception {
        return Result.success(giftService.PGift());
    }


}